﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExtendPropertyLib.WPF;
using ExtendPropertyLib;
using ExORMDesginer.Models;
using DatabaseSchemaReader.DataSchema;
namespace ExORMDesginer.UserControls
{
    public class FilterDbSchemaInfo : BusinessInfoBase<FilterDbSchemaInfo>
    {
        
        public static ExtendProperty SelectedTableItemsProperty = RegisterProperty(v => v.SelectedTableItems);
        public SerializableObservableCollection<IsCheckItem<DatabaseTable>> SelectedTableItems { set { SetValue(SelectedTableItemsProperty, value); } get { return (SerializableObservableCollection<IsCheckItem<DatabaseTable>>)GetValue(SelectedTableItemsProperty); } }

        
        public static ExtendProperty SelectedViewItemsProperty = RegisterProperty(v => v.SelectedViewItems);
        public SerializableObservableCollection<IsCheckItem<DatabaseView>> SelectedViewItems { set { SetValue(SelectedViewItemsProperty, value); } get { return (SerializableObservableCollection<IsCheckItem<DatabaseView>>)GetValue(SelectedViewItemsProperty); } }

        
        public static ExtendProperty SelectedSpItemsProperty = RegisterProperty(v => v.SelectedSpItems);
        public SerializableObservableCollection<IsCheckItem<DatabaseStoredProcedure>> SelectedSpItems { set { SetValue(SelectedSpItemsProperty, value); } get { return (SerializableObservableCollection<IsCheckItem<DatabaseStoredProcedure>>)GetValue(SelectedSpItemsProperty); } }

        
        public static ExtendProperty IsTableProperty = RegisterProperty(v => v.IsTable);
        public bool IsTable { set { SetValue(IsTableProperty, value); } get { return (bool)GetValue(IsTableProperty); } }

        
        public static ExtendProperty IsViewProperty = RegisterProperty(v => v.IsView);
        public bool IsView { set { SetValue(IsViewProperty, value); } get { return (bool)GetValue(IsViewProperty); } }

        
        public static ExtendProperty IsSpProperty = RegisterProperty(v => v.IsSp);
        public bool IsSp { set { SetValue(IsSpProperty, value); } get { return (bool)GetValue(IsSpProperty); } }



        public void Init(DatabaseSchema schema,List<string > selectedItemNames)
        {
            #region 初始化Filter view model;
            SelectedTableItems = new SerializableObservableCollection<IsCheckItem<DatabaseTable>>();
            SelectedViewItems = new SerializableObservableCollection<IsCheckItem<DatabaseView>>();
            SelectedSpItems = new SerializableObservableCollection<IsCheckItem<DatabaseStoredProcedure>>();
            if (selectedItemNames == null)
            {
                foreach (var table in schema.Tables)
                    SelectedTableItems.Add(new IsCheckItem<DatabaseTable>() { IsCheck = true, Item = table });

                foreach (var view in schema.Views)
                    SelectedViewItems.Add(new IsCheckItem<DatabaseView>() { IsCheck = true, Item = view });

                foreach (var sp in schema.StoredProcedures)
                    SelectedSpItems.Add(new IsCheckItem<DatabaseStoredProcedure>() { IsCheck = true, Item = sp });
            }
            else
            {
                foreach (var table in schema.Tables)
                {
                    bool isCheck = selectedItemNames.Contains(table.Name);

                    SelectedTableItems.Add(new IsCheckItem<DatabaseTable>() { IsCheck = isCheck, Item = table });
                }

                foreach (var view in schema.Views)
                {
                    bool isCheck = selectedItemNames.Contains(view.Name);
                    SelectedViewItems.Add(new IsCheckItem<DatabaseView>() { IsCheck = isCheck, Item = view });
                }

                foreach (var sp in schema.StoredProcedures)
                {
                    bool isCheck = selectedItemNames.Contains(sp.Name);
                    SelectedSpItems.Add(new IsCheckItem<DatabaseStoredProcedure>() { IsCheck = isCheck, Item = sp });
                }
                    
            }




            IsTable = true;
            #endregion
        }


        
        public static ExtendProperty ConditionProperty = RegisterProperty(v => v.Condition);
        /// <summary>
        /// 选择的条件
        /// </summary>
        public FilterConditionEnum Condition { set { SetValue(ConditionProperty, value); } get { return (FilterConditionEnum)GetValue(ConditionProperty); } }

        
        public static ExtendProperty ConditionTextProperty = RegisterProperty(v => v.ConditionText);
        /// <summary>
        /// 条件文本
        /// </summary>
        public string ConditionText { set { SetValue(ConditionTextProperty, value); } get { return (string)GetValue(ConditionTextProperty); } }
        

        /// <summary>
        /// 设置当前Tab状态
        /// </summary>
        public void SetActiveTabItemsFor(bool isCheck)
        {
            if (IsTable)
            {
                foreach(var item in SelectedTableItems)
                {
                    item.IsCheck = isCheck;
                }
            }
            if (IsView)
            {
                foreach (var item in SelectedViewItems)
                {
                    item.IsCheck = isCheck;
                }
            }
            if (IsSp)
            {
                foreach (var item in SelectedSpItems)
                {
                    item.IsCheck = isCheck;
                }
            }

        }

        /// <summary>
        /// 设置全部项目的IsChecked状态为指定状态。
        /// </summary>
        /// <param name="isCheck">状态</param>
        public void SetAllTabItemsFor(bool isCheck)
        {
            foreach (var item in SelectedTableItems)
            {
                item.IsCheck = isCheck;
            }
            foreach (var item in SelectedViewItems)
            {
                item.IsCheck = isCheck;
            }
            foreach (var item in SelectedSpItems)
            {
                item.IsCheck = isCheck;
            }

        }

        /// <summary>
        /// 根据条件设置全部项目的状态
        /// </summary>
        public void SetAllTabItemByConditionFor(bool isCheck)
        {
            Predicate<string> p = null;
            switch(this.Condition)
            {
                case FilterConditionEnum.Contain:
                    p = n=> n.Contains(ConditionText);
                    break;
                case FilterConditionEnum.Equal:
                    p = n => n == ConditionText;
                    break;
                case FilterConditionEnum.StartWith:
                    p = n => n.StartsWith(ConditionText);
                    break;
            }

            SelectedTableItems.Where(t=>p(t.Item.Name)).ToList().ForEach(item=>item.IsCheck = isCheck);
            SelectedViewItems.Where(t => p(t.Item.Name)).ToList().ForEach(item => item.IsCheck = isCheck);
            SelectedSpItems.Where(t => p(t.Item.Name)).ToList().ForEach(item => item.IsCheck = isCheck);

        }

        /// <summary>
        /// 获得被选择的名称列表
        /// </summary>
        /// <returns></returns>
        public List<String> GetSelectedNames()
        {
            List<string> liststr = new List<string>();
            if (SelectedTableItems != null && SelectedViewItems != null && SelectedSpItems != null)
            {
                liststr.AddRange(SelectedTableItems.Where(item => item.IsCheck).Select(item => item.Item.Name));
                liststr.AddRange(SelectedViewItems.Where(item => item.IsCheck).Select(item => item.Item.Name));
                liststr.AddRange(SelectedSpItems.Where(item => item.IsCheck).Select(item => item.Item.Name));
            }
            return liststr;
        }

    }
}
